var userDAL =require('../model/userDAL')
var qs = require('querystring')
var url = require('url')

var userController ={
    login:function(req,res){
        var userName = req.query.username
        var userpassword = req.query.userpassword
        var users={userName:userName,userpassword:userpassword}
        userDAL.login(users,function(err,results){
            if(err){
                console.log('数据查询错误')
                res.json({code:500,msg:'数据查询错误'})
            }else{
                console.log('查完数据')
                // console.log(results)
                if(results[0]){
                    console.log("1")
                    res.json({code:200,msg:'ok',data:1,results})
                }
                else{
                    res.json({code:200,msg:'ok',data:0})
                }
                
            }
        })
    },
    register: function (req, res) {
        // console.log('1')
        var userName = req.body.username
        var userpassword = req.body.userpassword
        var usertel = req.body.usertel
        var newusers={userName:userName,userpassword:userpassword,usertel:usertel}
        // console.log(newusers)
        userDAL.register(newusers,function(err,results){
            if(err){
                console.log('数据传输失败')
                res.json({code:500,msg:'数据传输失败'})
            }else{
                // console.log('数据插入成功')
                // console.log(results)
                if(results.affectedRows > 0){
                    console.log('注册成功')
                    res.json({code:200,msg:'ok',data:1,results})
                }
                else{
                    res.json({code:200,msg:'注册失败',data:0})
                }
                
            }
        })
    },
    checkName: function (req, res) {
        var userName = req.query.username
        console.log(userName)
        var checkusers={userName:userName}
        userDAL.checkName(checkusers,function(err,results){
            if(err){
                console.log('数据查询错误')
                res.json({code:500,msg:'数据查询错误'})
            }else{
                console.log('查完数据')
                if(results.length > 0){
                    console.log('chongfu')
                    res.end(JSON.stringify({ code: 200, message: '用户名重复' }))
                }
                else{
                    console.log('buchongfu')
                    res.end(JSON.stringify({ code: 200, message: '用户名不重复' }))
                }
                
            }
        })
    }
}
module.exports = userController